package com.base.core.util;

import com.base.core.error.ErrorUtils;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;


public final class CaptchaUtils {
	
	private static final CaptchaUtils captchaHelper = new CaptchaUtils();

    private final int WIDTH = 16;

    private final int HEIGHT = 22;

    private final int CODE_LENGTH = 4;

    private final String RAND_RANGE = "ABCDEFGHJKMNPQRSTUVWXYZ123456789";
    
    private final char[] CHARS = RAND_RANGE.toCharArray();
    
    private final String NUM_RAND_RANGE = "123456789";
    
    private final char[] NUM_CHARS = NUM_RAND_RANGE.toCharArray();

    private Random random = new Random();
    
    private CaptchaUtils() {
        // 
    }

    public static CaptchaUtils getInstance() {
        return captchaHelper;
    }

    public BufferedImage getImage(String captcha) {
        BufferedImage image = new BufferedImage(WIDTH * CODE_LENGTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = image.getGraphics();
        graphics.fillRect(0, 0, WIDTH * 4, HEIGHT);
        graphics.setColor(getRandColor(150,180));
        graphics.drawRect(0,0,WIDTH * CODE_LENGTH-1,HEIGHT-1);
        graphics.setColor(getRandColor(150, 200));
        for (int i = 0; i < 40; i++) {
        	 int x = random.nextInt(WIDTH * CODE_LENGTH);
 	        int y = random.nextInt(HEIGHT);
 	        int xl = random.nextInt(13);
 	        int yl = random.nextInt(15);
            graphics.drawLine(x, y, x+xl, y+yl);
        }
        graphics.setFont(new Font("Times New Roman", Font.PLAIN, 18));
        for (int i = 0; i < this.CODE_LENGTH; i++) {
            String temp = captcha.substring(i, i + 1);
            graphics.setColor(getRandColor(50, 100));
            graphics.drawString(temp, 13 * i + 6, 16);
        }
        graphics.dispose();
        return image;
    }

    public ByteArrayOutputStream getCaptchaBytes(String captcha) {
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        ImageOutputStream imageOut =null;
        try {
        	imageOut = ImageIO.createImageOutputStream(output);
            ImageIO.write(getImage(captcha), "JPEG", imageOut);
            return output;
        } catch (Exception e) {
            throw ErrorUtils.wrap(e);
        } finally{
            if(imageOut != null){
                try {
                	imageOut.close();
                } catch (IOException e) {
                    
                }
            }
        }
    }
    
    private Color getRandColor(int fc, int bc) {
    	if(fc > 255)
            fc = 255;
        if(bc > 255)
            bc = 255;
        int r = fc + random.nextInt(bc-fc-16);
        int g = fc + random.nextInt(bc-fc-14);
        int b = fc + random.nextInt(bc-fc-18);
        return new Color(r,g,b);
    }
    
    /**
     * 
     * @return 
     */
    public String getCaptcha(int length) {
        StringBuilder sb = new StringBuilder();
        int size = length < 1 ? CODE_LENGTH :length;
        for (int i = 0; i < size; i++)
            sb.append(CHARS[random.nextInt(CHARS.length)]);
        return sb.toString();
    }
    
    /**
     * 
     * @return
     */
    public String getCaptchaOnlyNum(int length) {
        StringBuilder sb = new StringBuilder();
        int size = length < 1 ? CODE_LENGTH :length;
        for (int i = 0; i < size; i++)
            sb.append(NUM_CHARS[random.nextInt(NUM_CHARS.length)]);
        return sb.toString();
    }
    
}
